package com.lifa.controller;

import com.lifa.dao.ChanpinsDao;
import com.lifa.entity.Chanpin;
import com.lifa.service.ChanpinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chanpin")
@Scope("prototype")
public class ChanpinController {
    @Autowired
    private ChanpinsDao chanpinDao;
    @Autowired
    private ChanpinService chanpinService;
    /**
     * 查询全部
     * @return
     */
    @RequestMapping("/chan")
    @ResponseBody
    public List<Chanpin> list(){
        return chanpinDao.query();
    }

    /**
     * 根据产品id删除产品信息
     * @param shangpin_Id
     * @return
     */
    @RequestMapping("/shanchu")
    @ResponseBody
    public int shanchu(int shangpin_Id){
        return chanpinService.delete(shangpin_Id);
    }

    /**
     * 添加产品信息
     * @param chanpin
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public int insert(Chanpin chanpin){
        return chanpinService.add(chanpin);
    }

    /**
     * 根据id修改产品信息
     * @param chanpin
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public int update(Chanpin chanpin){
        return chanpinService.xiugai(chanpin);
    }

    /**
     * 根据产品id查询
     * @param shangpin_Id
     * @return
     */
    @RequestMapping("/queryId")
    @ResponseBody
    public Chanpin queryId(int shangpin_Id){
        return chanpinService.queryId(shangpin_Id);
    }


    /**
     * 模糊加分页
     * @param name
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping("/queryByName")
    @ResponseBody
    public Map<String,Object> queryPageByName(String name,int offset,int limit){
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",chanpinDao.queryPage(name,offset,limit)) ;
        map.put("total",chanpinDao.getTotalCount(name)) ;
        return  map ;
    }

}
